import { pick } from '@/utils/helper';
import { getHead, mergeHead } from './common';

// Open graph protocol
function getArticleOGMeta(data) {
  return [
    {
      property: 'og:type',
      content: 'article'
    },
    {
      property: 'og:article:tag',
      content: data.keyword
    },
    {
      property: 'og:article:published_time',
      content: data.datePublished
    },
    {
      property: 'article:author',
      content: 'https://facebook.com/Tnaot.kh/'
    }
  ];
}

// Google Search Structure Data
function getArticleStructureDataMeta(data) {
  return [
    {
      type: 'application/ld+json',
      json: {
        '@context': 'https://schema.org',
        '@type': 'NewsArticle',
        mainEntityOfPage: {
          '@type': 'WebPage',
          '@id': data.canonicalPageUrl
        },
        ...pick({
          headline: 'title',
          image: 'thumb',
          datePublished: 'datePublished'
        })(data),
        author: {
          '@type': 'Organization',
          name: data.siteName,
          logo: {
            '@type': 'ImageObject',
            url: 'https://www.tnaot.com/img/logo-256.png'
          }
        },
        publisher: {
          '@type': 'Organization',
          name: data.siteName,
          logo: {
            '@type': 'ImageObject',
            url: 'https://www.tnaot.com/img/logo-256.png'
          }
        }
      }
    }
  ];
}

export default function getArticleHead(article) {
  const data = {
    ...article,
    ...pick({
      siteName: 'siteName',
      pageLocale: 'pageLocale',
      pageUrl: 'pageUrl',
      canonicalPageUrl: 'canonicalPageUrl',
      desktopUrl: 'desktopUrl',
      title: 'title',
      thumb: 'thumbs.0',
      keyword: 'keyword',
      description: 'description',
      datePublished: 'release_timestamp,date-iso'
    })(article)
  };

  return mergeHead(
    getHead(data),
    { script: [...getArticleStructureDataMeta(data)] },
    { meta: getArticleOGMeta(data) }
  );
}
